package Logic.dbUtils;

import org.junit.Test;

import java.io.File;
import java.util.*;

/**
 * @author liujun
 * @version V1.0
 * @date 2022/3/13 11:47 PM
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @description 文件处理
 */
public class FileHandler {

    public static Map<String, String> classPathMap;

    @Test
    public void test() {
        String name = GetClassPath("FileHandler");
        System.out.println(name);
    }

    public static String GetClassPath(String className) {
        if (className == null || className.length() == 0) {
            return null;
        }
        File directory = new File("");// 参数为空
        String courseFile = "";
        try {
            //courseFile = System.getProperty("user.dir");
            courseFile = directory.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        //System.out.println(courseFile);
        classPathMap = new HashMap<>();
        initClassPathMap(courseFile);
        /*for (String className : ClassPathMap.keySet()) {
            System.out.println("className: " + className + " ,path: " + ClassPathMap.get(className));
        }*/
        String prefixPath = System.getProperty("user.dir") + "/src/";
        String path = classPathMap.get(className + ".java")
                .replace(".java", "")
                .replace(prefixPath, "")
                .replace("/", ".");
        System.out.println(path);
        return path;
    }

    public static void initClassPathMap(String path) {
        List<String> files = new ArrayList<>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < Objects.requireNonNull(tempList).length; i++) {
            if (tempList[i].isFile()) {
                //文件名，不包含路径
                String fileName = tempList[i].getName();
                classPathMap.put(fileName, tempList[i].getPath());
            }
            if (tempList[i].isDirectory()) {
                initClassPathMap(tempList[i].getPath());
            }
        }
    }
}
